﻿using System;
using System.Security.Cryptography;
using System.Text;

public static class TripleDES
{

    private static readonly TripleDESCryptoServiceProvider DESProvider = new TripleDESCryptoServiceProvider();

    public const string KeyString = "Da3djxQT";

    public static string DES3Encrypt(string content, string key)
    {
        DESProvider.Key = Encoding.ASCII.GetBytes(key);
        DESProvider.Mode = CipherMode.CBC;
        DESProvider.Padding = PaddingMode.PKCS7;
        var desEncrypt = DESProvider.CreateEncryptor();
        var buffer = Encoding.ASCII.GetBytes(content);
        var cipherBytes = desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length);
        return Convert.ToBase64String(cipherBytes);
    }


    public static string DES3Decrypt(string content, string key)
    {
        try
        {
            DESProvider.Key = Encoding.ASCII.GetBytes(key);
            DESProvider.Mode = CipherMode.CBC;
            DESProvider.Padding = PaddingMode.PKCS7;
            var desEncrypt = DESProvider.CreateDecryptor();
            var buffer = Encoding.ASCII.GetBytes(content);
            var cipherBytes = desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length);
            return Convert.ToBase64String(cipherBytes);
        }
        catch (Exception e)
        {
            throw  new Exception(e.Message);
        }


    }
}

